#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include "TcpIpHeader.h"
#include "HttpParse.h"

// pcap
static char pcapErrorBuffer[PCAP_ERRBUF_SIZE];
static pcap_t *pcapHandle = NULL;
static int pcapDatalinkType = 0;


static void OpenPcapLive(void)
{
	char *ethPort = pcap_lookupdev(pcapErrorBuffer);

	if (ethPort == NULL) {
		printf("Error: could not loop up ethPort: %s\n", pcapErrorBuffer);
	}
	
	pcapHandle = pcap_open_live(ethPort, 65535, 1, 0, pcapErrorBuffer);
	
	if (pcapHandle == NULL) {
		printf("ERROR: could not open pcap live: %s\n", pcapErrorBuffer);
		exit(-1);
	}

	pcapDatalinkType = pcap_datalink(pcapHandle);
}


static void PcapPacketCallback(unsigned char *args, const struct pcap_pkthdr *header, const unsigned char *packet)
{
	PacketProcessing(packet, header->len);
}


static void RunPcapLoop(void)
{
	if (pcapHandle != NULL) {
		pcap_loop(pcapHandle, -1, &PcapPacketCallback, NULL);
	}
}

#if 1
int main(int argc, char **argv)
{
	OpenPcapLive();
	RunPcapLoop();	

	return 0;
}
#endif
